package lion.spider;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import lion.dev.web.Validator;
import lion.framework.core.db.DB;
import lion.framework.core.task.IAutoTask;
import lion.framework.core.task.anno.AutoTask;
import lion.spider.model.CrawlerConfig;

/**
 * @author lion
 * @email hl_0074@sina.com
 * @date 2016年5月6日
 */
@AutoTask
public class CrawlerTask implements IAutoTask {

	private Log log = LogFactory.getLog(CrawlerTask.class);

	@Override
	public void run(String args) {

		int id = Validator.toInt(args, -1);
		if (id < 0) {
			log.error("invalid crawler config id :" + id);
			return;
		}

		CrawlerConfig config = null;
		try (DB db = new DB()) {
			config = CrawlerConfig.load(id);
		}
		if (config == null) {
			log.error(" crawler config not found :" + id);
			return;
		}

		// start a travler
		new SpiderTravler().start(config);
	}
}
